<?php
/**
 * Product: rdc_analysis.
 * Date: 18-7-25
 * Time: 下午3:46
 */

namespace server;

use enums\HttpServerEnums;

trait WorkerStart
{
    private function phpSetting()
    {
        error_reporting( E_ALL );
        ini_set( 'display_errors', '1' );

        //将出错信息输出到一个文本文件
        ini_set( 'error_log', APP_ROOT . '/logs/php_error_log.txt' );
    }

    /**
     * worker process setting
     */
    private function workerProcessSettting()
    {
        if( PHP_OS == 'Linux' )
        {
            $user = posix_getpwnam( 'nobody' );
            posix_setgid( $user['gid']);
            posix_setuid( $user['uid']);
        }

    }

    private function setLoader()
    {

        $loader = new \Phalcon\Loader();
        $loader->registerNamespaces( array(
                                         'libraries' => APP_ROOT . 'libraries/',
                                         'utils'		=> APP_ROOT . 'apps/utils/',
                                         'apis'		=> APP_ROOT . 'apis',
                                         'enums'		=> APP_ROOT . 'enums/',
                                         'listeners'	=> APP_ROOT . 'listeners/',
                                         'helpers'   => APP_ROOT . 'helpers/'
                                     ));
        $loader->register();
    }

    private function composerAutoload()
    {
        require dirname( __DIR__ ) . '/vendor/autoload.php';
    }

    public function onWorkerStart( $server, $worker_id )
    {
        echo __FUNCTION__, PHP_EOL, PHP_EOL;


        define( 'APP_ROOT', dirname( __DIR__ ) . '/' );
        define( 'APP_MODE', 'dev' );

        $this->phpSetting();

        $this->workerProcessSettting();

        $this->setLoader();

        $this->composerAutoload();


        $di = new \Phalcon\DI\FactoryDefault();

        $this->loadCfg( $di );

        $di->httpServer = $this;

        $this->application = new \Phalcon\Mvc\Application($di);


        $this->registerModules();

        $this->_taskMgr->initTaskWorker( $server, $worker_id );

    }

    private function loadCfg( $di )
    {
        $arrInitCfg = array(
            array( 'name' => 'lockCfg', 'file' => 'lock.php', 'inDi' => true ),
            array( 'name' => 'sessionCfg', 'file' => 'session.php', 'inDi' => true ),
            array( 'name' => 'urlCfg', 'file' => 'url.php', 'inDi' => true ),
            array( 'name' => 'dbCfg', 'file' => 'db.php', 'inDi' => true ),
            array( 'name' => 'queueCfg', 'file' => 'queue.php', 'inDi' => true ),
            array( 'name' => 'config', 'file' => 'config.php', 'inDi' => true ),
            array( 'name' => 'logCfg', 'file' => 'log.php', 'inDi' => true ),
            array( 'name' => 'cacheCfg', 'file' => 'cache.php', 'inDi' => true ),
            array( 'name' => 'emailCfg', 'file' => 'email.php', 'inDi' => true ),
        );

        //set in di
        foreach( $arrInitCfg as $cfg )
        {
            if( file_exists( APP_ROOT . 'config/' . APP_MODE . '/' . $cfg[ 'file' ] ))
            {
                if( $cfg[ 'inDi' ] )
                {
                    $di->set( $cfg['name'], require APP_ROOT . 'config/' . APP_MODE . '/' . $cfg['file'], true );
                }
                else
                {
                    ${$cfg[ 'name' ]} = require APP_ROOT . 'config/' . APP_MODE . '/' . $cfg['file'];
                }
            }
            else
            {
                if( $cfg[ 'inDi' ] )
                {
                    $di->set( $cfg['name'], require APP_ROOT . 'config/' . $cfg['file'], true );
                }
                else
                {
                    ${$cfg[ 'name' ]} = require APP_ROOT . 'config/' . $cfg['file'];
                }
            }
        }


        @ini_set( 'session.name', HttpServerEnums::SESSION_KEY );

        require APP_ROOT . 'config/services.php';//加载公共services

    }

    public function registerModules()
    {
        // Register the installed modules
        $this->application->registerModules( array(
                                                 'common' =>
                                                     array(
                                                         'className' => 'apps\common\Module',
                                                         'path' => APP_ROOT . 'apps/common/Module.php'
                                                     ),
                                                 'fzq' =>
                                                     array(
                                                         'className' => 'apps\fzq\Module',
                                                         'path' => APP_ROOT . 'apps/fzq/Module.php'
                                                     ),
                                                 'tasksmgr' =>
                                                     array(
                                                         'className' => 'apps\tasksmgr\Module',
                                                         'path' => APP_ROOT . 'apps/tasksmgr/Module.php'
                                                     )

                                             ));
    }
}